import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            isShare:false,
            id: this.$router.params.chat_id,
            dongtai: {},
            transpondNum:'',
            newcommentNum:'',
            commentss: {
                offset: 0,
                length: 25,
                items: []
            },
            sub_commentss: {
                offset: 0,
                length: 25,
                items: [{
                    id: '',
                    reply_name: '',
                    content: '',
                    created_at: '',
                    comment_id: '',
                }]
            },
            sub_comment_amounts: {
                offset: 0,
                length: 25,
                items: [{
                    comment_id: '',
                }]
            },
            placeholder: '评论',
            comment_id: 0,
            replied_id: '',
            content: '',
        };
        return this.state;
    },
    getDongtai() {  
        var me = this;
        var data = {chat_id: this.state.id};
        app.get("web/dynamic_topic/dynamicDetail", data, function(res) {
            var dongtai = me.state.dongtai;
            dongtai = res.data.result;
            me.setState({
                dongtai: dongtai,
                transpondNum : dongtai = res.data.result.transpondNum,
            });
        });
    },
    listComments() {
        var me = this;
        var data = {offset: me.state.commentss.offset, length: me.state.commentss.length, chat_id: me.state.id};
        app.get("web/dynamic_topic/dynamicDetailComment", data, function(res) {
            var commentss = me.state.commentss;
            var list = res.data.result.list;
            commentss.items = commentss.items.concat(list);
            me.setState({
                total: res.data.result.total,
                commentss: commentss,
                newcommentNum : res.data.result.total,
            });
        });
        
    },
    listComment() {
        var me = this;
        var data = {offset: 0, length: 25, chat_id: me.state.id};
        app.get("web/dynamic_topic/dynamicDetailComment", data, function(res) {
            var commentss = me.state.commentss;
            var list = res.data.result.list;
            commentss.items = list;
            me.setState({
                total: res.data.result.total,
                commentss: commentss,
                newcommentNum: res.data.result.total,
            });
            me.commentss.offset = 0;
            me.commentss.length = 25;
        });
        
    },
    allSubComments() {
        var me = this;
        var data = {};
        var flag = true;
        if(me.beforeAllSubComments) {
            flag = me.beforeAllSubComments(data);
        }
        if(flag) {
            app.get("api/comments/all", data, function(res) {
                var data = [];
                for(var i = 0; i < res.data.result.data.length; i++) {
                    var m = {
                        id: res.data.result.data[i].id,
                        reply_name: res.data.result.data[i].reply_name,
                        content: res.data.result.data[i].content,
                        created_at: res.data.result.data[i].created_at,
                        comment_id: res.data.result.data[i].comment_id,
                    }
                    data.push(m);
                }
                me.setState({sub_commentss: {items: data}});
                if(me.afterAllSubComments) {
                    me.afterAllSubComments(res);
                }
            });
        }
    },
    allSubCommentAmount() {
        var me = this;
        var data = {};
        var flag = true;
        if(me.beforeAllSubCommentAmount) {
            flag = me.beforeAllSubCommentAmount(data);
        }
        if(flag) {
            app.get("api/comments/all", data, function(res) {
                var data = [];
                for(var i = 0; i < res.data.result.data.length; i++) {
                    var m = {
                        comment_id: res.data.result.data[i].comment_id,
                    }
                    data.push(m);
                }
                me.setState({sub_comment_amounts: {items: data}});
                if(me.afterAllSubCommentAmount) {
                    me.afterAllSubCommentAmount(res);
                }
            });
        }
    },
    componentWillMount () {

    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(content) {
        this.setState({
            content: content
        });
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidUpdate(){
        
    },
    componentDidShow () {
        this.listComment();
    },
    componentDidMount () {
        if(this.init) {
            this.init();
        }
    },
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
